#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        printf("替换前\n");
        execl("/usr/bin/ls", "ls", "-a", "-l", NULL);
        printf("替换后\n");
        int cnt = 3;
        while(cnt)
        {
            printf("I am child, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
            cnt--;
            sleep(1);
        }
        exit(1);
    }
    else if(id > 0)
    {
        int cnt = 5;
        while(cnt)
        {
            printf("I am father, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
            cnt--;
            sleep(1);
        }
        int status;
        pid_t ret = waitpid(id, &status, 0);
        if(ret == id)
        {
            printf("wait successful, ret: %d, WIFEXITED: %d, WEXITSTATUS: %d\n", ret, WIFEXITED(status), WEXITSTATUS(status));
        }
        sleep(5);
    }
    else 
    {
        printf("fork fail\n");
    }

    return 0;
}

